How to slide like a pro?¶

# This plot is not interactive
data = np.random.randn(2, 100)
fig, ax = plt.subplots()
ax.scatter(*data, c=data[1], s=100*np.abs(data[0]));
# This plot is interactive on your notebook only (but invisible on the slides)
%matplotlib widget
data = np.random.randn(2, 100)
fig, ax = plt.subplots()
ax.scatter(*data, c=data[1], s=100*np.abs(data[0]));
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/var/folders/tg/p4s0x3b55936405b1y1vws6h0000gn/T/ipykernel_50470/2200752080.py in <module>
      1 # This plot is interactive on your notebook only (but invisible on the slides)
----> 2 get_ipython().run_line_magic('matplotlib', 'widget')
      3 data = np.random.randn(2, 100)
      4 fig, ax = plt.subplots()
      5 ax.scatter(*data, c=data[1], s=100*np.abs(data[0]));

~/.pyenv/versions/3.8.12/envs/lewagon/lib/python3.8/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2349                 kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2350             with self.builtin_trap:
-> 2351                 result = fn(*args, **kwargs)
   2352             return result
   2353 

~/.pyenv/versions/3.8.12/envs/lewagon/lib/python3.8/site-packages/decorator.py in fun(*args, **kw)
    230             if not kwsyntax:
    231                 args, kw = fix(args, kw, sig)
--> 232             return caller(func, *(extras + args), **kw)
    233     fun.__name__ = func.__name__
    234     fun.__doc__ = func.__doc__

~/.pyenv/versions/3.8.12/envs/lewagon/lib/python3.8/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/.pyenv/versions/3.8.12/envs/lewagon/lib/python3.8/site-packages/IPython/core/magics/pylab.py in matplotlib(self, line)
     97             print("Available matplotlib backends: %s" % backends_list)
     98         else:
---> 99             gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
    100             self._show_matplotlib_backend(args.gui, backend)
    101 

~/.pyenv/versions/3.8.12/envs/lewagon/lib/python3.8/site-packages/IPython/core/interactiveshell.py in enable_matplotlib(self, gui)
   3531                 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
   3532 
-> 3533         pt.activate_matplotlib(backend)
   3534         configure_inline_support(self, backend)
   3535 

~/.pyenv/versions/3.8.12/envs/lewagon/lib/python3.8/site-packages/IPython/core/pylabtools.py in activate_matplotlib(backend)
    325     from matplotlib import pyplot as plt
    326 
--> 327     plt.switch_backend(backend)
    328 
    329     plt.show._needmain = False

~/.pyenv/versions/3.8.12/envs/lewagon/lib/python3.8/site-packages/matplotlib/pyplot.py in switch_backend(newbackend)
    275     backend_name = cbook._backend_module_name(newbackend)
    276 
--> 277     class backend_mod(matplotlib.backend_bases._Backend):
    278         locals().update(vars(importlib.import_module(backend_name)))
    279 

~/.pyenv/versions/3.8.12/envs/lewagon/lib/python3.8/site-packages/matplotlib/pyplot.py in backend_mod()
    276 
    277     class backend_mod(matplotlib.backend_bases._Backend):
--> 278         locals().update(vars(importlib.import_module(backend_name)))
    279 
    280     required_framework = _get_required_interactive_framework(backend_mod)

~/.pyenv/versions/3.8.12/lib/python3.8/importlib/__init__.py in import_module(name, package)
    125                 break
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128 
    129 

~/.pyenv/versions/3.8.12/lib/python3.8/importlib/_bootstrap.py in _gcd_import(name, package, level)

~/.pyenv/versions/3.8.12/lib/python3.8/importlib/_bootstrap.py in _find_and_load(name, import_)

~/.pyenv/versions/3.8.12/lib/python3.8/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

~/.pyenv/versions/3.8.12/lib/python3.8/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

~/.pyenv/versions/3.8.12/lib/python3.8/importlib/_bootstrap.py in _gcd_import(name, package, level)

~/.pyenv/versions/3.8.12/lib/python3.8/importlib/_bootstrap.py in _find_and_load(name, import_)

~/.pyenv/versions/3.8.12/lib/python3.8/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

ModuleNotFoundError: No module named 'ipympl'

How to display interactive charts?¶

  • Use plotly !
  • If the chart below does not display on slides after rendering, try to run the cell below to force javascript injection of plotly inside your notebook JSON.
# Only pyplot can easily make a graph interactive in notebook AND slides
import plotly.express as px
df = px.data.gapminder()
fig = px.scatter(df.query("year==2007"), x="gdpPercap", y="lifeExp", size="pop", color="continent",
           hover_name="country", log_x=True, size_max=60)
fig.show()

How to Remove inputs?¶

The following fragment 👇 should only show the dataframe output because the cell "tag" is set to "remove_input"

lift benefit
0 0.01 20000.0
1 0.02 40000.0
2 0.03 60000.0
3 0.04 80000.0

How to Remove outputs?¶

The following fragment 👇 should only show the cell input because the cell "tag" is set to "remove_output"

# Show this but not the result
1+1